package pseudoAssemblerCompiler.twopassassembler.view.util;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;

import java.util.ArrayList;
import java.util.List;

/**
 * Контейнер для создании таблиц
 *
 * User: Роман
 * Date: 23.05.12
 * Time: 16:40
 */
public class TableViewerContainer {

    /**
     * Контейнер для таблицы
     */
    private Composite _composite;

    public Composite getComposite() {
        return _composite;
    }

    public TableViewer get_tableViewer() {
        return _tableViewer;
    }

    public List<TableViewerColumn> get_columns() {
        return _columns;
    }

    /**
     * Таблица для просмотра горячих клавиш
     */
    private TableViewer _tableViewer;

    /**
     * Список колонок
     */
    private List<TableViewerColumn> _columns = new ArrayList<TableViewerColumn>();

    private boolean _isFirstCall = true;

    private List<Integer> _columnsWidth = new ArrayList<Integer>();

    public TableViewerContainer(Composite parent) {
        _composite = new Composite(parent, SWT.NONE);
        _tableViewer = new TableViewer(_composite, SWT.FULL_SELECTION);
        layouts();
    }

    public TableViewerColumn createColumn(String aColumnName) {
        return createColumn(aColumnName, 0);
    }

    public TableViewerColumn createColumn(String aColumnName, int width) {
        TableViewerColumn column = new TableViewerColumn(_tableViewer, SWT.NONE);
        column.getColumn().setText(aColumnName);
        column.getColumn().setWidth(width);
        _columnsWidth.add(width);
        _columns.add(column);
        return column;
    }

    private void layouts() {
        FormData formData;
        //Temporary
        formData = new FormData();
        formData.left = new FormAttachment(0, 0);
        formData.top = new FormAttachment(0, 0);
        formData.right = new FormAttachment(100, 0);
        formData.bottom = new FormAttachment(100, 0);
        _composite.setLayout(new FormLayout());
        _composite.setLayoutData(formData);

        //Temporary
        formData = new FormData();
        formData.left = new FormAttachment(0, 0);
        formData.top = new FormAttachment(0, 0);
        formData.right = new FormAttachment(100, 0);
        formData.bottom = new FormAttachment(100, 0);
        _tableViewer.getTable().setLayoutData(formData);
    }

    public void listeners() {
        getComposite().addControlListener(new ControlAdapter() {
            public void controlResized(ControlEvent e) {
                Table table = _tableViewer.getTable();

                Rectangle area = getComposite().getClientArea();
                Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
                int width = area.width - 2 * table.getBorderWidth();
                if (preferredSize.y > area.height + table.getHeaderHeight()) {
                    // Subtract the scrollbar width from the total column width
                    // if a vertical scrollbar will be required
                    Point vBarSize = table.getVerticalBar().getSize();
                    width -= vBarSize.x;
                }
                Point oldSize = table.getSize();

                if (oldSize.x > area.width) {
                    // table is getting smaller so make the _columns
                    // smaller first and then resize the table to
                    // match the client area width
                    setColumnsWidth(width);
                    table.setSize(area.width, area.height);
                } else {
                    // table is getting bigger so make the table
                    // bigger first and then make the _columns wider
                    // to match the client area width
                    table.setSize(area.width, area.height);
                    setColumnsWidth(width);
                }
            }
        });
    }

    private void setColumnsWidth(int tableWidth) {

        TableViewerColumn[] tvc =
                get_columns().toArray(new TableViewerColumn[get_columns().size()]);
        int columnWidth;
        int countZeroWidthColumn = 0;

        for (int i = 0; i < _columnsWidth.size(); i++) {
            if (0 != _columnsWidth.get(i)) {
                tableWidth -= tvc[i].getColumn().getWidth();
            } else {
                countZeroWidthColumn++;
            }
        }

        if (0 == countZeroWidthColumn) {
            return;
        }
        columnWidth = tableWidth / countZeroWidthColumn;

        for (int i = 0; i < tvc.length; i++) {
            if (0 == _columnsWidth.get(i)) {
                tvc[i].getColumn().setWidth(columnWidth);
            }
        }
        tvc[tvc.length - 1].getColumn().setWidth(tableWidth - ((countZeroWidthColumn - 1) * columnWidth));

    }

}
